package com.wodan.zhongjinjicang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wodan.zhongjinjicang.common.exception.BusinessException;
import com.wodan.zhongjinjicang.common.redis.JedisClient;
import com.wodan.zhongjinjicang.common.utils.JsonUtils;
import com.wodan.zhongjinjicang.common.utils.PasswordDes;
import com.wodan.zhongjinjicang.enums.BusinessErrorEnum;
import com.wodan.zhongjinjicang.frame.AbstractClientApiJsonBusinessMethodService;
import com.wodan.zhongjinjicang.mapper.ThirdUserMapper;
import com.wodan.zhongjinjicang.mapper.UserMapper;
import com.wodan.zhongjinjicang.model.ThirdUserBean;
import com.wodan.zhongjinjicang.model.ThirdUserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat.PHONE;


/**
 * 用户登录
 */
@Service("thirdLogin")
public class ThirdLoginService extends AbstractClientApiJsonBusinessMethodService {

    private static final String USID = "usid";
    private static final String CHANNEL = "channel";

    @Autowired
    private ThirdUserMapper thirdUserDao;

    @Override
    protected JSONObject doExecute(JSONObject data,String sessionId) {

        ThirdUserBean thirdUserBean = JsonUtils.jsonToPojo(data.toJSONString(), ThirdUserBean.class);

        ThirdUserBean thirdUser = thirdUserDao.selectByChannel(thirdUserBean.getUsid(), thirdUserBean.getChannel());
        Map<String , Object> modelMap = new HashMap<String , Object>();
        //老用户读取出userId，新用户直接创建一个userId返回
        if(thirdUser!=null)
            modelMap.put("userId", thirdUser.getId());
        else{
            thirdUserDao.insert(thirdUserBean);
            modelMap.put("userId", thirdUserBean.getId());
        }
        return new JSONObject(modelMap);
    }

    @Override
    protected boolean isNeedLogin() {
        return false;
    }

    @Override
    protected String[] getRequiredParams() {
        return new String[]{USID,CHANNEL};
    }

}
